package com.mxx.qh.activity;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.mxx.qh.R;
import com.mxx.qh.utils.PackageUtils;
import com.mxx.qh.utils.PermissionUtil;

public class SplashActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_version;
    private TextView tv_jump;
    private TextView tv_wx;
    private final String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE
            , Manifest.permission.READ_EXTERNAL_STORAGE
            , Manifest.permission.ACCESS_COARSE_LOCATION
            , Manifest.permission.ACCESS_FINE_LOCATION
    };
    private PermissionUtil.IPermissionListener listener;
    private int time = 3;
    private final Handler mHandler = new Handler();
    private final Runnable mRunnable = new Runnable() {
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            mHandler.removeCallbacks(mRunnable);
            time--;
            if (time == 0) {
                skipToMain();
            } else {
                tv_jump.setText(time + "s跳过");
                mHandler.postDelayed(mRunnable, 1000);
            }
        }
    };

    private void skipToMain() {
        Intent mIntent = new Intent(this, MainActivity.class);
        startActivity(mIntent);
        finish();
        //取消界面跳转时的动画，使启动页的logo图片与注册、登录主页的logo图片完美衔接
        overridePendingTransition(0, 0);
    }

    @Override

    protected int getLayoutResId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void setStatusBar() {
        ImmersionBar
                .with(this)
                .fitsSystemWindows(false)
                .statusBarColor(android.R.color.transparent)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initView() {
        tv_jump = findViewById(R.id.tv_jump);
        tv_version = findViewById(R.id.tv_version);
        tv_wx = findViewById(R.id.tv_wx);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        String version_name= PackageUtils.getVersionName(this);
        if (version_name!=null){
            tv_version.setText("版本:"+version_name);
            tv_version.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        tv_jump.setOnClickListener(this);
        tv_wx.setOnClickListener(this);
        listener = new PermissionUtil.IPermissionListener() {
            @Override
            public void allPermissionGranted() {
                startCountdown();
            }

            @Override
            public void partPermissionRefuse() {
                Toast.makeText(SplashActivity.this, "权限被拒绝，请到设置里面开启对应的权限", Toast.LENGTH_SHORT).show();
                startCountdown();
            }
        };
    }

    private void startCountdown() {
        mHandler.postDelayed(mRunnable, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        PermissionUtil.getInstance().requestPermission(this, listener, permissions);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtil.getInstance().handlerActivityResult(requestCode, grantResults);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_jump) {
            time = 1;
            mHandler.postDelayed(mRunnable, 0);
        }
    }

    /**
     * 屏蔽物理返回键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            //If token is null, all callbacks and messages will be removed.
            mHandler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }

}